﻿#if NETFX || NETCORE

using Apewer.Internals.Interop;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Apewer.Surface
{

    /// <summary>由 .NET Framework 提供的标准窗体。</summary>
    public class NormalForm : System.Windows.Forms.Form
    {

        /// <summary>必需的设计器变量。</summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>清理所有正在使用的资源。</summary>
        /// <param name="disposing">如果应释放托管资源，为 true；否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null)) components.Dispose();
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.ClientSize = new System.Drawing.Size(900, 600);
            this.Font = FormsUtility.DefaultFont;
            this.Text = "NormalForm";
            this.ResumeLayout(false);
        }

        #endregion

        /// <summary>构造函数。</summary>
        public NormalForm()
        {
            InitializeComponent();
        }

        /// <summary>获取 DPI 缩放比例。</summary>
        public double DpiScale
        {
            get
            {
                if (FormsUtility.DpiScale == null)
                {
                    using (var g = CreateGraphics())
                    {
                        FormsUtility.DpiScale = g.DpiX / 96F;
                    }
                }
                return FormsUtility.DpiScale.Value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            // WM_DPICHANGED = 0x02E0
            base.WndProc(ref m);
        }

        #region Aero

        private bool AeroEnabled = false;

        /// <summary>启用 Aero 透明效果。</summary>
        private void EnableAero()
        {
            AeroEnabled = true;
            Load += (s, e) =>
            {
                if (!FormsUtility.DwmIsCompositionEnabled) return;

                var width = Width;
                var height = Height;

                var margins = new MARGINS();
                // margins.Right = -1; // 全窗体透明。
                margins.Right = margins.Left = margins.Top = margins.Bottom = width + height;
                DwmApi.DwmExtendFrameIntoClientArea(Handle, ref margins);
            };
        }

        /// <summary></summary>
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
            if (AeroEnabled)
            {
                if (FormsUtility.DwmIsCompositionEnabled)
                {
                    e.Graphics.Clear(Color.Black);
                }
            }
        }

        #endregion

    }

}

#endif
